package com.company.LinkList;

import java.lang.reflect.Array;
import java.util.Arrays;

/**
 * @author VX5
 * @Title: MJC
 * @ProjectName DataStructure
 * @Description: TODO
 * @date ${DAT}9:57
 */
public class Josepfu2 {
    public static void main(String[] args) {
        DoPerson doPerson = new DoPerson();
        doPerson.add(5);
        doPerson.show();
        doPerson.JosepfuOut(1,3);
    }

    static class Person{
        private int no;
        private Person next;


        public Person(int no) {
            this.no = no;
        }

        public int getNo() {
            return no;
        }

        public void setNo(int no) {
            this.no = no;
        }

        public Person getNext() {
            return next;
        }

        public void setNext(Person next) {
            this.next = next;
        }

        @Override
        public String toString() {
            return "Person{" +
                    "no=" + no +
                    '}';
        }
    }

    static class DoPerson{
        private static Person first = null;
        //添加
        public void add(int num){

            Person curperson = null;
            if (num < 1){
                System.out.println("人数不能为1");
            }else {
                for (int i = 1; i <= num; i++){
                    System.out.println("*");
                    if (first == null){
                        first = new Person(i);
                        first.next = first;
                        curperson = first;
                    }else {
                        Person person = new Person(i);
                        curperson.next = person;
                        person.next = first;
                        curperson = person;
                    }
                }
            }
        }

        //遍历
        public void show(){
            if (first!=null) {
                Person curPeron = first;
//                System.out.println(first.getNo());
                while (true) {
                    System.out.println(curPeron.getNo());
                    curPeron = curPeron.next;
                    if (curPeron == first){
                        break;
                    }
                }
            }else {
                System.out.println("链表为空");
            }
        }

        //约瑟夫环

        /**
         * num 开始的数
         * @param num
         * @param index
         */
        public void JosepfuOut(int num,int index){
            Person curperson = first;
            Person prepreson = first;//这样如果这样跟进来赋值的话会出问题 所以先将prepreson放在链表倒数第一个节点上
            while (true){
                if (prepreson.next == first){
                    break;
                }
                prepreson = prepreson.next;
            }

            if (curperson!=null){
                while (true){
                    if (curperson.getNo() == num){
                        break;
                    }
                    prepreson = prepreson.next;//prepreson始终指向临时对象的上一个
                    curperson = curperson.next;

                }
            }
            //开始数数
            while (true) {
                if (curperson == curperson.next){
                    System.out.println("剔除：" + curperson.getNo());
                    break;
                }
                for (int i = 0; i < index-1; i++) {
                    prepreson = prepreson.next;
                    curperson = curperson.next;
                }
                System.out.println("剔除：" + curperson.getNo());
                curperson = curperson.next;
                prepreson.next = curperson;
            }
        }
    }
}
